package com.yylending.las.lending.quota.uploadimg;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;

import java.util.Map;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Header;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import rx.Observable;

/**
 * Created by laixj on 2016/11/17.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class UploadImgModel extends Model {

	private static final String TAG = "UploadImgModel";

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class, 30);//设置超时时间
		return service.uploadImage(Global.LOGIN_TOKEN, ((UploadImage)params).type, ((UploadImage)params).part);
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		UploadResult result = cast(obj);
		EventAgent.post(result);
	}

	public interface Service {
		/**
		 * 上传图片接口
		 */
		@Multipart
		@POST("sys/upload/image")
		Observable<UploadResult> uploadImage(@Header("token") String token, @Part("type") RequestBody type, @Part MultipartBody.Part file);
	}

	public static class UploadImage {
		//需要上传的文件
		MultipartBody.Part part;
		RequestBody type;

		UploadImage(MultipartBody.Part part, RequestBody type) {
			this.part = part;
			this.type = type;
		}
	}

	static class UploadResult extends BaseResultEntity<Map<String, String>> {
	}

	static class UploadFailure {
		String filePath;
		String tips;
		String requestPage;
	}


	static class UploadCancel {
		String filePath;
		String tips;
		String requestPage;
	}

	/**
	 * 上传图片Param
	 */
	static class UploadImg {
		String filename;
		public String type;

		UploadImg(String filename, String type) {
			this.filename = filename;
			this.type = type;
		}
	}

	/**
	 * 上传图片
	 */
	/*public static void uploadImgByBytes(RxAppCompatActivity aty, byte[] bytes, String filename, String type, OnNextListener listener) {
		UploadImgRequest request = new UploadImgRequest(aty, UploadImgRequest.METHOD_UPLOAD_IMG, listener);
		UploadImg entity = new UploadImg(filename, type);
		RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), bytes);
		MultipartBody.Part part = MultipartBody.Part.createFormData("files", filename, requestBody);
		request.uploadImg = entity;
		request.part = part;
		CLog.i("", "上传图片名称-request.uploadImg.type->" + request.uploadImg.type);
		CLog.i("", "上传图片名称-request.uploadImg.filename->" + request.uploadImg.filename);
		NetworkHelper.execute(request);
	}*/
}